package com.liyingtao.servlet;

import com.liyingtao.bean.Admin;
import com.liyingtao.bean.Board;
import com.liyingtao.bean.News;
import com.liyingtao.biz.NewsService;
import com.liyingtao.util.GetNowTime;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/addNewsServlet")
public class addNewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title=request.getParameter("title");
        String context = request.getParameter("context");
        HttpSession session=request.getSession();
        Admin admin= (Admin) session.getAttribute("admin");
        String  time=GetNowTime.getNowTime();
        int topicId=Integer.parseInt(request.getParameter("topicid"));
        News news=new News(title,context,time,new Board(topicId),admin);

        NewsService newsservice=new NewsService();
        int flag=newsservice.addNews(news);
        if(flag>0){
            response.sendRedirect("toMainPageServlet");
        }else {
            response.sendRedirect("addNews.jsp");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
